# !/usr/bin python3
# encoding: utf-8 -*-
# @file     : notice_res.py
# @author   : 沙陌 Matongxue_2
# @Time     : 2023/10/22 13:31
# @Copyright: 北京码同学

# 获取某一次jenkins任务的执行结果
import sys

import jsonpath
import requests

from common.file_load import load_yaml_file


class JenkinsStatus:

    def send(self,build_url):
        url = f'{build_url}/api/json'
        method = 'get'
        resp = requests.request(method=method,url=url)
        return resp

class WxNotice:

    def send(self,job_name,build_number,result,user,build_url):
        res = load_yaml_file('result.yml')
        url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8daddcd7-eb1d-40ea-8157-90147b45e1aa'
        method = 'post'
        json = {
            "msgtype": "markdown",
            "markdown": {
                "content": f"#### {job_name}测试完成 \n "
                           f"- 任务:第{build_number}次 \n "
                           f"- 状态:{result} \n "
                           f"- 用例总数:{res['total']} \n "
                           f"- 成功: {res['passed']} \n "
                           f"- 失败: {res['failed']} \n "
                           f"- 错误: {res['error']} \n "
                           f"- 跳过: {res['skipped']} \n "
                           f"- 执行人:{user} \n "
                           f"[查看报告]({build_url}/allure)"
            }
        }
        resp = requests.request(method=method,url=url,json=json)
        return resp

class FeiShuNotice:

    def send(self,job_name,build_number,result,user,build_url):
        self.res = load_yaml_file('result.yml')
        url = 'https://open.feishu.cn/open-apis/bot/v2/hook/b78ec614-0108-4a81-b752-6c4fe1d32eda'
        method = 'post'
        json = {
                "msg_type": "post",
                "content": {
                        "post": {
                                "zh_cn": {
                                        "title": f"{job_name}第{build_number}次测试完成",
                                        "content": [
                                            [{
                                                "tag": "text",
                                                "text": f"状态:{result}"
                                            }],
                                            [{
                                                "tag": "text",
                                                "text": f"用例总数:{self.res['total']}"
                                            }],
                                            [{
                                                "tag": "text",
                                                "text": f"成功:{self.res['passed']}"
                                            }],
                                            [{
                                                "tag": "text",
                                                "text": f"失败:{self.res['failed']}"
                                            }],
                                            [{
                                                "tag": "text",
                                                "text": f"跳过:{self.res['skipped']}"
                                            }],
                                            [{
                                                "tag": "text",
                                                "text": f"错误:{self.res['error']}"
                                            }],
                                            [{
                                                "tag": "text",
                                                "text": f"执行人:{user}"
                                            }],
                                            [{
                                                "tag": "a",
                                                "text": "查看报告",
                                                "href": f"{build_url}/allure"
                                            }],

                                        ]
                                }
                        }
                }
        }
        resp = requests.request(method=method,
                                url=url,
                                json=json)
        return resp

class DingDingNotice:
    def send(self,job_name,build_number,result,user,build_url):
        self.res = load_yaml_file('result.yml')
        url = 'https://oapi.dingtalk.com/robot/send?access_token=eda69535b902dfc3f009aba101c407cf11e1e68e758b194d4a232f42f1ad1aca'
        method = 'post'
        json = {
            "msgtype": "markdown",
            "markdown": {
                "title":f"### {job_name}测试完成",
                "text": f"### {job_name}测试完成 \n - 任务：第{build_number}次\n - 状态：{result} \n - 用例总数: {self.res['total']} \n - 成功: {self.res['passed']} \n - 失败: {self.res['failed']} \n - 错误: {self.res['error']} \n - 跳过: {self.res['skipped']} \n - 执行人: {user}  \n[查看报告]({build_url}/allure) "
            }
        }
        resp = requests.request(method=method,
                         url=url,
                         json = json)

if __name__ == '__main__':
    args = sys.argv # 获取外部入参，第一个是build_url，第二个是通知类型
    build_url = args[1]
    notice_type = args[2]
    # build_url = 'http://192.168.0.188:8080/job/apiautest20231022/2'
    resp = JenkinsStatus().send(build_url)
    print(resp.json())
    user = jsonpath.jsonpath(resp.json(),'$..shortDescription')[0]
    print(user)
    result = jsonpath.jsonpath(resp.json(),'$..result')[0]
    print(result)
    fullDisplayName = jsonpath.jsonpath(resp.json(),'$..fullDisplayName')[0]
    job_name =fullDisplayName.split('#')[0].strip()
    print(job_name)
    build_number = fullDisplayName.split('#')[1]
    print(build_number)
    if notice_type == 'wx':
        WxNotice().send(job_name,build_number,result,user,build_url)
    elif notice_type == 'dd':
        DingDingNotice().send(job_name, build_number, result, user, build_url)
    elif notice_type == 'feishu':
        FeiShuNotice().send(job_name, build_number, result, user, build_url)